﻿Global.namespace("System.Web.Page");
System.Web.Page.Register=function(config){
    this.userId=config.userId;
    
    config.templateFile="register.htm?t="+ Global.random();
    System.Web.Page.Register.superclass.constructor.call(this, config);
}
Global.extend(System.Web.Page.Register,System.Core.UI.BasePage,{
    onLoading:function(){
        if($('#home_Loading').length>=0){ return; }
        Global.showLoadingMask({
            wrapper:'#'+this.id,
            id:'home_Loading'
        });
    },
    onRendered:function(){
        Global.hideLoadingMask('home_Loading');
        
        var ctr=this;
        $('#btnSend').click(function(){
            if(!ctr.validateInput()){ return;}
            ctr.submit();
        });
    },
    validateInput:function(){
        var emailPattern="";
        if($.trim($('#txtEmail').val())===''){
            $('#txtEmail').addClass("ValidateError");
            return false;
        }else{
            $('#txtContent').removeClass("ValidateError");
        }
        return true;
    },
    submit:function(){
        var ctr=this;
        var email=$('#txtEmail').val();
        var url=StringHandler.format(
            "{0}InlineService.axd?action=Register&email={1}&userId={2}",
            Config.rootServiceURL,
            email,
            this.userId
        );
        System.Core.Handler.RequestHandler.get(
            url,
            {},
            function(response){
                var data = System.Core.Handler.RequestHandler.getResponseData(response);
                var data=StringHandler.format("({0})",data);
                var result=eval(data);
                // return nothing
                if(!result){ return;}
                if(result.error===true){
                    
                    $('.Error').html(result.msg);
                    // action when error while submmited data
                    return;
                }
                // set reference to current user
                System.Web.Runtime.currentUser=result;
                // load home page
                ctr.parent.loadControl(System.Web.Page.Home, { wrapper: '.MasterPageWrapper',autoRender:true});
            }
        );
    }
});

